package stocks.client.gui.model;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.JOptionPane;

import stocks.client.gui.view.buySellFrame;
import stocks.client.gui.view.loginFrame;
import stocks.client.gui.view.mainFrame;
import stocks.client.gui.view.registerFrame;
import stocks.client.gui.view.viewFrame;
import stocks.client.gui.view.withdrawFrame;
import stocks.server.CallBack;
import stocks.server.StockServerInterface;

public class model extends UnicastRemoteObject implements CallBack{

	private buySellFrame bsFrame;
	private loginFrame lFrame;
	private registerFrame rFrame;
	private mainFrame mFrame;
	private viewFrame vFrame;
	private withdrawFrame wFrame;
	private String username;
	
	private Registry reg;
	private StockServerInterface server;
	
	public model() throws RemoteException
	{
		bsFrame = new buySellFrame(this);
		lFrame = new loginFrame(this);
		rFrame = new registerFrame(this);
		mFrame = new mainFrame(this); //create and save all frames
		vFrame = new viewFrame(this);
		wFrame = new withdrawFrame(this);
		
		try {
			reg = LocateRegistry.getRegistry("localhost",1099);
			server = (StockServerInterface)reg.lookup("StocksServer");
			server.registerClient(this);
			System.out.println("Client registered");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Return Buy/Sell frame, no arguments
	 */
	public buySellFrame getBuySellFrame()
	{
		return bsFrame;
	}
	
	/**
	 * Return login frame, no arguments
	 */
	public loginFrame getLoginFrame()
	{
		return lFrame;
	}
	
	/**
	 * Return register frame, no arguments
	 */
	public registerFrame getRegisterFrame()
	{
		return rFrame;
	}
	
	/**
	 * Return main frame, no arguments
	 */
	public mainFrame getMainFrame()
	{
		return mFrame;
	}
	
	/**
	 * Return view frame, no arguments
	 */
	public viewFrame getViewFrame()
	{
		return vFrame;
	}
	
	/**
	 * Return withdraw frame, no arguments
	 */
	public withdrawFrame getWithdrawFrame()
	{
		return wFrame;
	}
	
	/**
	 * Return RMI interface, no arguments
	 */
	public StockServerInterface getRMIInterface()
	{
		return server;
	}

	@Override
	public void showMessage(String msg) throws RemoteException {
		
		JOptionPane.showMessageDialog(null, msg, msg, JOptionPane.ERROR_MESSAGE);
	}

	@Override
	public String getName() throws RemoteException {
		// TODO Auto-generated method stub
		return username;
	}

	@Override
	public void joinedMessage(String from) throws RemoteException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void loginSucsess(String message) throws RemoteException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void transactionCompleteBuy(String message) throws RemoteException {
		// TODO Auto-generated method stub
		JOptionPane.showMessageDialog(null, message, message, JOptionPane.ERROR_MESSAGE);
	}

	@Override
	public void transactionCompleteSold(String message) throws RemoteException {
		// TODO Auto-generated method stub
		JOptionPane.showMessageDialog(null, message, message, JOptionPane.ERROR_MESSAGE);
	}

	@Override
	public void transactionFailed(String message) throws RemoteException {
		// TODO Auto-generated method stub
		
	}
	
	public void setName(String name)
	{
		this.username = name;
	}
}
